package com.demo.config.security.jump;

import com.demo.commons.constant.ConStantsR;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>失败跳转</p>
 *@author zwy
 *@date 2024/1/17 11:07
*/
public class ErrorAuthentication implements AuthenticationFailureHandler {
    private ObjectMapper om=new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        System.out.println("登入失败了");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset-utf-8");
        String json=null;
        /**
         * 模拟json
         */
        json= "{\"code\":500,\"msg\":"+ ConStantsR.LoginFailed+"\"}";
        response.getWriter().write(json);
        response.getWriter().flush();
    }

}
